// pages/home/home.js
import * as api from '../../utils/api.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    banner_list: [],
    mobile: '',
    product_list: [],
    system_notice_list: [],
    tmplIds: [],
    domain:api.domain,
    cityID: 0,
    cityName: '所在城市',
    isLogin: false
  },
  checkLogin() {
    const token = wx.getStorageSync('token')
    const that = this
    if (token) {
      api.checkToken()
        .then(res => {
          that.setData({
            isLogin: true
          })
        })
        .catch(err => console.log(err))
    }
  },
  loginModal() {
    if (!this.data.isLogin) {
      wx.showModal({
        title: '提示',
        content: '请先登录',
        confirmText: '去登录',
        cancelText: '再看看',
        success (res) {
          if (res.confirm) {
            wx.switchTab({
              url: '/pages/myinfo/myinfo',
            })
          } else if (res.cancel) {
            wx.showToast({
              title: '您选择了取消',
              icon: 'error'
            })
          }
        }
      })
      return false
    } else {
      return true
    }
  },
  nextinstall:function(){
    if(!this.loginModal()) return false;
    wx.navigateTo({
      url: '/pages/order/installOrder/index',
    })
  },
  nextrepair:function(){
    if(!this.loginModal()) return false;
    wx.navigateTo({
      url: '/pages/home/repair',
    })
  },
  nextupkeep:function(){
    if(!this.loginModal()) return false;
    wx.navigateTo({
      url: '/pages/home/maintenance',
    })
  },
  product:function(){
    // console.log(1);
    wx.switchTab({
      url: '/pages/product/product',
    })
  },
  nextcombox:function(e){
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/product/details?id=' + id,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getPageData()
  },

  getPageData(city_id = 0) {
    const that = this
    api.indexData(city_id)
      .then(res => {
        console.log(res)
        if (res.code == 1) {
          const data = res.data
          that.setData({
            banner_list: data.banner_list,
            mobile: data.mobile,
            product_list: data.product_list,
            system_notice_list: data.system_notice_list,
            tmplIds: data.tmplIds
          })
        } else {

        }
      })
      .catch(err => console.log(err));
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.checkLogin()
    const loginType = wx.getStorageSync('loginType')
    if(loginType == 'business') {
      wx.reLaunch({
        url: '/pages/business/index',
      })
    }
    if(loginType == 'worker') {
      wx.reLaunch({
        url: '/pages/worker/index',
      })
    }
    this.setCityData()
  },

  setCityData (city_id = 0, city_name = null) {
    const that = this
    const cityId = city_id ? city_id : wx.getStorageSync('cityId')
    const cityName = city_name ? city_name : wx.getStorageSync('cityName')
    if (cityId && that.data.cityID == 0) {
      that.setData({
        cityId
      })
      that.getPageData(cityId)
    } 
    if (cityName && that.data.cityName == '所在城市') {
      that.setData({
        cityName
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  toArticle(e) {
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../articleDetail/articleDetail?id=' + id,
    })
  },

  callPhone() {
    const that = this
    const mobile = that.data.mobile
    if (mobile) {
      wx.makePhoneCall({
        phoneNumber: mobile
      })
    } else {
      const cityId = wx.getStorageSync('cityId')
      if (cityId) {
        wx.showToast({
          title: '未设置救援电话',
          icon: 'error'
        })
      } else {
        that.getFuzzyLocation('callPhone', that.callPhone)
      }
    }
  },

  getFuzzyLocation(call_function = '', callback = null){
    console.log({call_function})
    const that = this
    const cityId = wx.getStorageSync('cityId')
    if (cityId) return false;
    wx.getFuzzyLocation({
      type: 'wgs84',
      success (res) {
        const latitude = res.latitude
        const longitude = res.longitude
        console.log({latitude, longitude})
        api.getAddress(latitude, longitude)
          .then(res => {
            const cityName = res.data.city_name
            const cityId = res.data.city_info.id
            wx.setStorageSync('cityName', cityName)
            wx.setStorageSync('cityId', cityId)
            that.setData({
              cityId,
              cityName
            })
            if (callback) callback()
          }).catch(err => console.log(err))
      },
      fail (err) {
        wx.showModal({
          title: '提示',
          content: '您未同意提供模糊地理位置信息，我们无法为您展示最近的客服服务信息',
          cancelText: '拒绝授权',
          confirmText: '去授权',
          complete: (res) => {
            if (res.cancel) {
              wx.showToast({
                title: '您拒绝了授权',
                icon: 'error'
              })
            }
        
            if (res.confirm) {
              wx.openSetting({
                success (res) {
                  if (res.authSetting["scope.userFuzzyLocation"]) {
                    that.getFuzzyLocation('getFuzzyLocation|openSetting')
                  } else {
                    
                  }
                }
              })
            }
          }
        })
      }
     })
  },
})